package cn.lili.controller.feign.system;

import cn.lili.modules.system.client.DictionaryClient;
import cn.lili.modules.system.client.SettingClient;
import cn.lili.modules.system.constants.DictConstant;
import cn.lili.modules.system.entity.dos.Dictionary;
import cn.lili.modules.system.entity.dos.Setting;
import cn.lili.modules.system.service.DictionaryService;
import cn.lili.modules.system.service.SettingService;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import lombok.RequiredArgsConstructor;
import org.antlr.runtime.UnwantedTokenException;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

/**
 * DictionaryFeignController
 *
 * @author Chopper
 * @version v1.0
 * 2021-11-08 11:47
 */
@RestController
@RequiredArgsConstructor
public class DictionaryFeignController implements DictionaryClient {

    private final DictionaryService dictionaryService;

    @Override
    public List<Dictionary> getListByCode(String code) {
        return dictionaryService.dictionryListByCode(code, null);
    }

    /**
     * 添加字典
     * @param dictionary 字典
     * @return 是否成功
     */
    @Override
    public Boolean save(Dictionary dictionary) {
        dictionary.setStatus(DictConstant.STATUS_1);
        return dictionaryService.save(dictionary);
    }

    /**
     * 修改了盲盒前缀同步修改字典前缀
     * @param oldMhPrefix 老前缀
     * @param newMhPrefix 新前缀
     * @return 是否成功
     */
    @Override
    public Boolean updateValueName(String oldMhPrefix, String newMhPrefix) {
        LambdaUpdateWrapper<Dictionary> dictionaryLambdaUpdateWrapper = new LambdaUpdateWrapper<>();
        dictionaryLambdaUpdateWrapper.set(Dictionary::getValueName, newMhPrefix);
        dictionaryLambdaUpdateWrapper.eq(Dictionary::getValueName, oldMhPrefix);
        return dictionaryService.update(dictionaryLambdaUpdateWrapper);
    }

    /**
     * 获取字典
     * @param dictionary 字典条件
     * @return 字典
     */
    @Override
    public Dictionary getDict(Dictionary dictionary) {
        LambdaQueryWrapper<Dictionary> queryWrapper = new LambdaQueryWrapper<Dictionary>(dictionary);
        queryWrapper.eq(Dictionary::getStatus, DictConstant.STATUS_1);
        queryWrapper.last("limit 1");
        return dictionaryService.getOne(queryWrapper);
    }

    /**
     * 每次调用把对应的key值加1
     * @return 加1之后的值
     */
    @Override
    public String autoIncrement(@RequestParam String valueName, @RequestParam String code) {
        LambdaQueryWrapper<Dictionary> queryWrapper = new LambdaQueryWrapper<Dictionary>();
        queryWrapper.eq(Dictionary::getValueName, valueName);
        queryWrapper.eq(Dictionary::getCode, code);
        queryWrapper.eq(Dictionary::getStatus, DictConstant.STATUS_1);
        queryWrapper.last("limit 1");
        Dictionary dictionary = dictionaryService.getOne(queryWrapper);
        if (dictionary == null) {
            return "";
        }
        int value = Integer.parseInt(dictionary.getValue()) + 1;
        dictionary.setValue(value+"");
        dictionaryService.updateById(dictionary);

        //往前面补0  例如 ：1 ==> 00001
        StringBuffer buffer = new StringBuffer(dictionary.getValue());
        int count = 5 - buffer.length();
        for (int i = 0; i < count; i++) {
            buffer.insert(0, "0");
        }
        return valueName + String.valueOf(buffer);
    }
}
